<template>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: 'homepage' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <el-row :gutter="30">
            <el-col :span="8">
                <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodsList">
                    <template #append>
                        <el-button icon="Search" @click="getGoodsList" />
                    </template>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="goAddPage">添加商品</el-button>
            </el-col>
        </el-row>
        <!-- 表格区域 -->
        <el-table :data="goodsList" border stripe>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="goods_name" label="商品名称" width="400px"></el-table-column>
            <el-table-column prop="goods_price" label="商品价格(元)" width="180"></el-table-column>
            <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
            <el-table-column prop="add_time" label="创建时间">
                <template #default="{ row }">
                    {{ $dayjs(row.add_time * 1000).format('YYYY-MM-DD HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="250px">
                <template #default="{ row }">
                    <!-- {{ row }} -->
                    <!-- 修改 -->
                    <el-button type="primary" size="small">修改</el-button>
                    <!-- 删除 -->
                    <el-button type="danger" size="small" @click="removeGoodsById(row.goods_id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination v-model:current-page="queryInfo.pagenum" v-model:page-size="queryInfo.pagesize"
            :page-sizes="[5, 10, 15, 20]" layout="total, sizes, prev, pager, next, jumper" :total="total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </el-card>
</template>
<script>
import { ElMessageBox } from 'element-plus'
export default {
    data() {
        return {
            goodsList: [],
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            total: 0,
        }
    },
    created() {
        this.getGoodsList()
    },
    methods: {
        async getGoodsList() {
            let data = await this.$axios.get('goods', {
                params: this.queryInfo
            })
            console.log(data)
            if (data.data.meta.status !== 200) {
                return this.$message.error("获取商品列表失败")
            } else {
                this.goodsList = data.data.data.goods
                this.total = data.data.data.total
                console.log(data)
            }
        },
        // 监听 页码值 改变
        handleCurrentChange(newPage) {
            console.log(newPage)
            this.queryInfo.pagenum = newPage
            this.getGoodsList()
        },
        // 监听分页 改变
        handleSizeChange(newSize) {
            console.log(newSize)
            this.queryInfo.pagesize = newSize
            this.getGoodsList()
        },
        //
        async removeGoodsById(id) {
            let confirmResult = await ElMessageBox.confirm(
                '此操作将永久删除该商品，是否继续？', {
                confirmButtonText: '确定',
                cancelButtonText: '拒绝',
                type: 'warning',
            }
            ).catch(err => err)
            if (confirmResult !== 'confirm') {
                return this.$message.info("已取消删除")
            }
            let data = await this.$axios.delete('goods/' + id)
            if (data.data.meta.status !== 200) {
                return this.$message.error("删除商品失败")
            } else {
                this.$message.success("删除商品成功")
                this.getGoodsList()
            }
        },
        goAddPage() {
            this.$router.push('/goods/add')
        }
    }
}
</script>


<style scoped>
.el-table{
    margin: 12px 0;
}
</style>